package com.tianji.aigc.controller;

import com.tianji.aigc.domain.vo.PageResultVO;
import com.tianji.aigc.service.RedisVectorStoreService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/embedding")
@RequiredArgsConstructor
public class EmbeddingController {

    private final RedisVectorStoreService redisVectorStoreService;

    /**
     * 保存向量数据（使用 JSON Body 提交）
     */
    @PostMapping
    public void saveVectorStore(@RequestBody List<String> messages) {
        redisVectorStoreService.saveDocuments(messages);
    }

    /**
     * 真分页查询（不依赖 topK）
     */
    @GetMapping("/list")
    public PageResultVO<Document> listDocuments(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        List<Document> docs = redisVectorStoreService.listDocuments(page, size);
        long total = redisVectorStoreService.countDocuments();
        return new PageResultVO<>(docs, (int) total, page, size);
    }

    /**
     * 删除指定文档
     */
    @DeleteMapping("/{id}")
    public void deleteDocument(@PathVariable String id) {
        redisVectorStoreService.deleteDocument(id);
    }
}
